package com.inhim.pjhim.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.inhim.pjhim.R;
import com.inhim.pjhim.entity.ProjectListEntity;
import com.inhim.pjhim.entity.ReaderStyle;
import com.inhim.pjhim.entity.ReaderTypeList;
import com.inhim.pjhim.utils.ImageLoaderUtils;
import com.inhim.pjhim.view.CustomRoundAngleImageView;

import org.yczbj.ycrefreshviewlib.adapter.RecyclerArrayAdapter;
import org.yczbj.ycrefreshviewlib.holder.BaseViewHolder;

import fm.jiecao.jcvideoplayer_lib.JCUtils;

public class ProjectListAdapter extends RecyclerArrayAdapter<ProjectListEntity.List> {

    private ReaderStyle.List readerStyle;
    private ReaderTypeList.List readerType;
    private Activity context;
    public ProjectListAdapter(Activity context) {
        super(context);
        this.context=context;
    }
    public void  setReaderStyle(ReaderStyle.List readerStyle){
        this.readerStyle=readerStyle;
    }
    public void  setReaderType(ReaderTypeList.List readerType){
        this.readerType=readerType;
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        PersonViewHolder personViewHolder =new PersonViewHolder(parent,viewType);
        if(viewType==0&&readerStyle!=null){
            View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_project_list_title,parent,false);
            TitleHolder titleHolder=new TitleHolder(view);
            ViewGroup viewGroup= (ViewGroup) personViewHolder.itemView;
            LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT
            );
            ImageLoaderUtils.setImage(readerStyle.getReaderStyleValue().getCover(),titleHolder.iv_title);
            titleHolder.tv_num.setText("共"+readerStyle.getTotal()+"篇文章");
            titleHolder.tv_content.setText(readerStyle.getReaderStyleValue().getSynopsis());
            titleHolder.tv_title.setText(readerStyle.getReaderStyleValue().getValue());
            titleHolder.iv_back.setOnClickListener(v -> context.finish());
            viewGroup.addView(view,0,layoutParams);
            return personViewHolder;
        }
        /*if(viewType==0&&readerType!=null){
            View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_project_type_list_title,parent,false);
            TitleTypeHolder titleHolder=new TitleTypeHolder(view);
            ViewGroup viewGroup= (ViewGroup) personViewHolder.itemView;
            LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT
            );
            ImageLoaderUtils.setImage(readerType.getIcon(),titleHolder.iv_title);
            titleHolder.tv_title.setText(readerType.getName());
            titleHolder.iv_back.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    context.finish();
                }
            });
            viewGroup.addView(view,0,layoutParams);
            return personViewHolder;
        }*/
        return personViewHolder;
    }
    class TitleHolder extends RecyclerView.ViewHolder {
        public TextView tv_title;
        public TextView tv_content;
        public TextView tv_num;
        public ImageView iv_title,iv_back;
        public TitleHolder(View itemView) {
            super(itemView);
            tv_title=itemView.findViewById(R.id.tv_title);
            tv_content=itemView.findViewById(R.id.tv_content);
            tv_num=itemView.findViewById(R.id.tv_num);
            iv_title=itemView.findViewById(R.id.iv_title);
            iv_back=itemView.findViewById(R.id.iv_back);
        }
    }

    class TitleTypeHolder extends RecyclerView.ViewHolder {
        public TextView tv_title;
        public ImageView iv_back;
        private CustomRoundAngleImageView iv_title;
        public TitleTypeHolder(View itemView) {
            super(itemView);
            tv_title=itemView.findViewById(R.id.tv_title);
            iv_title=itemView.findViewById(R.id.iv_title);
            iv_back=itemView.findViewById(R.id.iv_back);
        }
    }

    public class PersonViewHolder extends BaseViewHolder<ProjectListEntity.List> {

        ImageView iv_title;
        TextView tv_title;
        TextView tv_time1,tv_time;
        TextView tv_content;
        private TextView textview2;
        int mPosition;
        PersonViewHolder(ViewGroup parent,int position){
            super(parent, R.layout.item_project_list);
            mPosition=position;
            iv_title = getView(R.id.iv_title);
            tv_title = getView(R.id.tv_title);
            tv_time1 = getView(R.id.tv_time1);
            tv_content = getView(R.id.tv_content);
            textview2=getView(R.id.textview2);
            tv_time=getView(R.id.tv_time);
        }
        @Override
        public void setData(ProjectListEntity.List data) {
            super.setData(data);
            tv_content.setText(data.getContent());
            ImageLoaderUtils.setImage(data.getCover(),iv_title);
            tv_title.setText(data.getTitle());
            if(data.getTagName()==null || "".equals(data.getTagName())){
                textview2.setVisibility(View.GONE);
            }else{
                textview2.setVisibility(View.VISIBLE);
                textview2.setText(data.getTagName());
            }
            //tv_content.setText("上传时间:"+data.getCreateTime());
            /*if(data.getAuthor()!=null && data.getAuthor().getName()!=null &&! "".equals(data.getAuthor().getName())){
                tv_time1.setVisibility(View.VISIBLE);
                tv_time1.setText("作者:" + data.getAuthor().getName());
            }else {
                tv_time1.setVisibility(View.GONE);
            }*/
            tv_time1.setVisibility(View.GONE);
            //1文章 2视频 3音频
            if(data.getType().equals("1")){
                tv_time.setVisibility(View.GONE);
            }else{
                if(data.getDuration()!=null){
                    tv_time.setVisibility(View.VISIBLE);
                    tv_time.setText("时长 "+JCUtils.stringForTime2(Integer.valueOf(data.getDuration())));
                }else{
                    tv_time.setText("时长 00:00");
                }
            }
        }
    }
}
